﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BusinessModel;
using EntityDataModel;
using System.ComponentModel;
using System.Data.Objects.DataClasses;
using BaseUtilities;

namespace Controller.DataProvider
{
    public class UnitChangePro : GenericProvider<UnitChange>
    {
        public UnitChangePro()
        {
            EntityName = "Tbl_UnitChange";
        }

        #region override Methods
        //----------------------------------------
        public override EntityObject CreateEntity()
        {
            return new Tbl_UnitChange();
        }
        //----------------------------------------
        public override UnitChange CreateModel()
        {
            return new UnitChange();
        }
        //----------------------------------------
        public override void ToEntity(UnitChange pModel, EntityObject pEntity, params EnumFieldCopy[] pEnumFieldCopys)
        {
            Tbl_UnitChange oEntity = pEntity as Tbl_UnitChange;
            oEntity.ProductId = pModel.ProductId;
            oEntity.UnitId = pModel.UnitId;
            oEntity.Level = pModel.Level;
            oEntity.Quantity = pModel.Quantity;
            oEntity.SalePriceUnit = pModel.SalePriceUnit;
            oEntity.IsWholeSale = pModel.IsWholeSale;
            oEntity.ApplyDate = pModel.ApplyDate;
            pEnumFieldCopys.ToList().ForEach((item) =>
            {
                switch (item)
                {
                    case EnumFieldCopy.Id:
                        oEntity.Id = pModel.Id;
                        break;
                    case EnumFieldCopy.Created:
                        oEntity.CreatedBy = pModel.CreatedBy;
                        oEntity.CreatedDate = pModel.CreatedDate;
                        break;
                    case EnumFieldCopy.Modified:
                        oEntity.ModifiedBy = pModel.ModifiedBy;
                        oEntity.ModifiedDate = pModel.ModifiedDate;
                        break;
                    case EnumFieldCopy.All:
                        oEntity.Id = pModel.Id;
                        oEntity.CreatedBy = pModel.CreatedBy;
                        oEntity.CreatedDate = pModel.CreatedDate;
                        oEntity.ModifiedBy = pModel.ModifiedBy;
                        oEntity.ModifiedDate = pModel.ModifiedDate;
                        break;
                }
            });
        }
        //----------------------------------------
        public override void ToModel(EntityObject pEntity, UnitChange pModel, params EnumFieldCopy[] pEnumFieldCopys)
        {
            Tbl_UnitChange oEntity = pEntity as Tbl_UnitChange;
            pModel.ProductId = oEntity.ProductId;
            pModel.UnitId = oEntity.UnitId;
            pModel.Level = oEntity.Level;
            pModel.Quantity = oEntity.Quantity;
            pModel.SalePriceUnit = oEntity.SalePriceUnit;
            pModel.IsWholeSale = oEntity.IsWholeSale;
            pModel.ApplyDate = oEntity.ApplyDate;
            pEnumFieldCopys.ToList().ForEach((item) =>
            {
                switch (item)
                {
                    case EnumFieldCopy.Id:
                        pModel.Id = oEntity.Id;
                        break;
                    case EnumFieldCopy.Created:
                        pModel.CreatedBy = oEntity.CreatedBy;
                        pModel.CreatedDate = oEntity.CreatedDate;
                        break;
                    case EnumFieldCopy.Modified:
                        pModel.ModifiedBy = oEntity.ModifiedBy;
                        pModel.ModifiedDate = oEntity.ModifiedDate;
                        break;
                    case EnumFieldCopy.Relation:
                        if (oEntity.Tbl_Product != null)
                        {
                            pModel.ProductName = oEntity.Tbl_Product.Name;
                            pModel.ProductCode = oEntity.Tbl_Product.Code;
                        }
                        if (oEntity.Tbl_Unit != null)
                            pModel.UnitName = oEntity.Tbl_Unit.Name;
                        break;
                    case EnumFieldCopy.All:
                        pModel.Id = oEntity.Id;
                        pModel.CreatedBy = oEntity.CreatedBy;
                        pModel.CreatedDate = oEntity.CreatedDate;
                        pModel.ModifiedBy = oEntity.ModifiedBy;
                        pModel.ModifiedDate = oEntity.ModifiedDate;
                        if (oEntity.Tbl_Product != null)
                        {
                            pModel.ProductName = oEntity.Tbl_Product.Name;
                            pModel.ProductCode = oEntity.Tbl_Product.Code;
                        }
                        if (oEntity.Tbl_Unit != null)
                            pModel.UnitName = oEntity.Tbl_Unit.Name;
                        break;
                }
            });
        }
        #endregion override Methods
        #region Public Methods
        //----------------------------------------
        public ResultModel GetsByProductId(int pProductId)
        {
            IList<UnitChange> result = new List<UnitChange>();
            using (ManageStoreEntities entities = new ManageStoreEntities())
            {
                List<Tbl_UnitChange> lst = entities.Tbl_UnitChange.Where(p => p.ProductId == pProductId).ToList();
                lst.ForEach((item) =>
                {
                    UnitChange oUnitChange = new UnitChange();
                    ToModel(item, oUnitChange, EnumFieldCopy.All);
                    result.Add(oUnitChange);
                });
            }
            return new ResultModel(result);
        }
        //----------------------------------------
        public ResultModel GetsByProductId1(int pProductId, DateTime pApplyDate)
        {
            IList<UnitChange> result = null;
            using (ManageStoreEntities entities = new ManageStoreEntities())
            {
                List<Tbl_UnitChange> lst = (from p in entities.Tbl_UnitChange.Where(p => p.ProductId == pProductId)
                          select p).AsEnumerable().Where(p => !p.ApplyDate.HasValue || p.ApplyDate.Value.Date <= pApplyDate.Date).ToList();
                lst.ForEach((item) =>
                {
                    UnitChange oUnitChange = new UnitChange();
                    ToModel(item, oUnitChange, EnumFieldCopy.All);
                    result.Add(oUnitChange);
                });
            }
            return new ResultModel(result);
        }
        //----------------------------------------
        public ResultModel GetsByProductId(int pProductId, DateTime pApplyDate)
        {
            IList<UnitChange> result = null;
            using (ManageStoreEntities entities = new ManageStoreEntities())
            {
                result = (from p in entities.Tbl_UnitChange.Where(p => p.ProductId == pProductId && p.SalePriceUnit.HasValue)
                          select new UnitChange
                          {
                              Id = p.Id,
                              UnitName = p.Tbl_Unit.Name,
                              UnitId = p.UnitId,
                              Level = p.Level,
                              Quantity = p.Quantity,
                              SalePriceUnit = p.SalePriceUnit,
                              ApplyDate = p.ApplyDate
                          }).AsEnumerable().Where(p => !p.ApplyDate.HasValue || p.ApplyDate.Value.Date <= pApplyDate.Date).ToList();
            }
            return new ResultModel(result);
        }
        #endregion Public Methods
    }
}
